24-3 媬P牬郋Bz

若要顯示某一個資料夾的大小,可以使用,範例如下:

原始檔(dirSize01.js):(灰色區域按兩下即可拷貝)
// 顯示檔案夾的大小

fso = new ActiveXObject("Scripting.FileSystemObject");
folderPath = "c:\\windows\\system32";	// 會有權限問題
folderPath = "d:\\users\\jang\\matlab\\toolbox\\machineLearning";
folderObj = fso.GetFolder(folderPath);
WScript.Echo(folderPath + " 目錄的大小是 " + folderObj.Size + " bytes.");

執行上述程式後,典型顯示結果如下:

c:\windows\system32 目錄的大小是 1116543626 bytes.

另一個常見的應用,則是管理者要檢查每個人在 d:/users 下面所佔用的磁碟大小,可使用下列範例來達成:

原始檔(dirSize02.js):(灰色區域按兩下即可拷貝)
// Get directory sizes under d:/users

topDirName="d:/users";
fso = WScript.CreateObject("Scripting.FileSystemObject");
dirObj = fso.GetFolder(topDirName);

subFolderList=new Enumerator(dirObj.SubFolders);
for (subFolderList.moveFirst(); !subFolderList.atEnd(); subFolderList.moveNext()){
	dirName=subFolderList.item().name;
	dirSize=subFolderList.item().Size;
	WScript.Echo(dirName+" ===> "+dirSize+" bytes");
}

執行上述程式後,典型顯示結果如下:

classifiedEmailBackup ===> 4737324602 bytes jang ===> 54129835620 bytes jang2 ===> 91708923441 bytes Windows Live Mail ===> 16105564241 bytes (請注意:由於上述範例必須檢查每一個子目錄所佔用的空間,所以執行時間會比較久。)

我們可以使用 WSH 來顯示目前工作目錄,或是改變目前工作目錄,如下:

原始檔(dir01.js):(灰色區域按兩下即可拷貝)
// 使用 WSH 來顯示目前工作目錄,及改變目前工作目錄

wshShell=new ActiveXObject("WScript.Shell");
WScript.Echo("目前工作目錄:"+wshShell.CurrentDirectory);
wshShell.CurrentDirectory = "c:\\windows\\temp";
WScript.Echo("改變目前工作目錄至:"+wshShell.CurrentDirectory);

典型顯示結果如下:

目前工作目錄:D:\users\jang\books\wsh\example 改變目前工作目錄至:c:\windows\temp

下列這個範例,列出 c:\windows\temp 目錄下的所有檔案,如下:

原始檔(fileList01.js):(灰色區域按兩下即可拷貝)
// 列出一個特定目錄下的所有檔案

fso = new ActiveXObject("Scripting.FileSystemObject");
folderPath="c:\\windows\\temp";
fsoFolder = fso.GetFolder(folderPath);
fileList = new Enumerator(fsoFolder.Files);
WScript.Echo("Files under \""+folderPath+"\":");
for (fileList.moveFirst(); !fileList.atEnd(); fileList.moveNext())
	WScript.Echo(fileList.item());

由於輸出較長,在此不贅列,讀者請自行試看看此範例。

下列這個範例,列出磁碟機及其相關性質:

原始檔(driveList01.js):(灰色區域按兩下即可拷貝)
// 列出所有的磁碟機

fso = new ActiveXObject("Scripting.FileSystemObject");
driveTypes=["未知類型", "抽取式", "硬碟", "網路磁碟機", "光碟", "虛擬磁碟"];
drives = new Enumerator(fso.Drives);		// Create Enumerator on Drives.
for (; !drives.atEnd(); drives.moveNext()) {	// Enumerate drives collection.
	x = drives.item();
	WScript.Echo(x.DriveLetter+":")
	WScript.Echo("\tx.DriveType = " + x.DriveType + " (" + driveTypes[x.DriveType] + ")");
	WScript.Echo("\tx.ShareName = " + x.ShareName);
	WScript.Echo("\tx.IsReady = " + x.IsReady);
	if (x.IsReady){
		WScript.Echo("	x.VolumeName = " + x.VolumeName);
		WScript.Echo("	x.AvailableSpace = " + x.AvailableSpace + " Bytes");
	}
}

典型顯示結果如下:

C: x.DriveType = 2 (硬碟) x.ShareName = x.IsReady = true x.VolumeName = x.AvailableSpace = 24901296128 Bytes D: x.DriveType = 2 (硬碟) x.ShareName = x.IsReady = true x.VolumeName = 新增磁碟區 x.AvailableSpace = 17471188992 Bytes E: x.DriveType = 4 (光碟) x.ShareName = x.IsReady = true x.VolumeName = hp LaserJet 3800 x.AvailableSpace = 0 Bytes ...
JScript 程式設計與應用:用於單機的 WSH 環境